package entity.text;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：mmzs
 * @date ：Created in 2020/9/16 23:30
 * @description：
 * @modified By：
 * @version: $
 */
public class Citation implements Text {

    public String index;

    public List<String> citationList;

    public Citation() {
    }

    public void extractorCitationParagraph(Paper paper) {
        this.citationList = new ArrayList<>();
        String pattern = "\\[(\\d+,)?" + index + "(,\\d+)?\\]";
        paper.chapters.forEach(a -> {
            Matcher m = Pattern.compile(pattern).matcher(a.content);
            while (m.find()) {
                int start = m.start();
                int end = m.end();
                String citation = null;
//                当前行
                int[] currentLineIndex = readLineCurrent(a.content, start);
                String currentLineText = a.content.substring(currentLineIndex[0], currentLineIndex[1]);
                start = currentLineIndex[0];
                end = currentLineIndex[1];

                int lastLineStart = readLineReverse(a.content, start);
                String lastLineText = a.content.substring(lastLineStart, start);

                int nextLineEnd = readLineAlong(a.content, end);
                String nextLineText = a.content.substring(end, nextLineEnd);
//                你这个nowline又是啥？一个逐行跳的指针
                String nowLine = currentLineText;
//                两个条件：1、当前行小写字母开头；2、上一行不是句号问号感叹号结尾；
//                两条件满足一个就继续循环
//                再加个条件，字数要够
//                再加个条件，是否到达段落边缘
                while (!"".equals(lastLineText) &&
                        ( Character.isLowerCase(nowLine.charAt(0))
                                || !(lastLineText.endsWith(".\n") || lastLineText.endsWith(". \n") || lastLineText.endsWith("?\n") || lastLineText.endsWith("!\n"))
                                || lastLineText.length() > 0.75*paper.lineLength)
                ){
                    start = lastLineStart;
                    nowLine = lastLineText;
                    lastLineStart = readLineReverse(a.content, start);
                    lastLineText = a.content.substring(lastLineStart, start);
                }
//                向下搜，同样的方法，再来一次
                nowLine = currentLineText;
                while (!"".equals(nextLineText) &&
                        (Character.isLowerCase(nextLineText.charAt(0))
                                || !(nowLine.endsWith(".\n") || nowLine.endsWith(". \n") || nowLine.endsWith("?\n") || nextLineText.endsWith("!\n"))
                                || nowLine.length() > 0.75*paper.lineLength)
                ){
                    end = nextLineEnd;
                    nowLine = nextLineText;
                    nextLineEnd = readLineAlong(a.content, end);
                    nextLineText = a.content.substring(end, nextLineEnd);
                }

                citation = a.content.substring(start, end);
                this.citationList.add(citation);
            }
        });
    }

    public void extractorCitationSentence(Paper paper) {
        //todo
        this.citationList = new ArrayList<>();
        String pattern = "\\[(\\d+,)?" + index + "(,\\d+)?\\]";
        paper.chapters.forEach(a -> {
            String s = a.content;
            Matcher m = Pattern.compile(pattern).matcher(s);
            while (m.find()) {
                int start = m.start();
                int end = m.end();
                String citation = null;

                String patternFront = "(. [A-Z0-9])(?=(^. [A-Z0-9])*)";
                Matcher m1 = Pattern.compile(patternFront).matcher(s.substring(0, start));
                if (m1.find()) {
                    start = m1.start()+1;
                } else {
                    start = 0;
                }

                String patternBack = ". [A-Z0-9]";
                Matcher m2 = Pattern.compile(patternBack).matcher(s);
                if (m2.find(end)) {
                    end = m2.start() + 1;
                } else {
                    end = s.length() - 1;
                }
                citation = s.substring(start, end);
                citationList.add(citation);
            }
        });
    }

    public int readLineReverse(CharSequence input, int endIndex) {
        int start = endIndex;
        if (input.charAt(start - 1) == '\n') {
            start--;
        }
        do {
            start--;
        } while (input.charAt(start) != '\n');
        return start + 1;
    }

    public int readLineAlong(String inputText, int startIndex) {
        int end = startIndex;
        if (inputText.charAt(end + 1) == '\n') {
            end++;
        }
        do {
            end++;
        } while (inputText.charAt(end) != '\n');
        return end + 1;
    }

    public int[] readLineCurrent(CharSequence inputText, int index) {
        int start = index;
        do {
            start--;
        } while (inputText.charAt(start) != '\n');
        int end = index;
        do {
            end++;
        } while (inputText.charAt(end) != '\n');
        int[] result = new int[2];
        result[0] = start + 1;
        result[1] = end + 1;
        return result;
    }
}
